#include <iostream>
#include <sys/stat.h>

namespace TW_FileTool
{
    class FileTool
    {
        public:
            FileTool(std::string v_path)
            :   m_path(v_path), 
                m_file(NULL),
                m_is_open(false)
            {
            }
            ~FileTool()
            {
                if(m_is_open)
                {
                    fclose(m_file);
                    m_file = NULL;
                    m_is_open = false;
                }
            }
        
        public:
            // 打开文件
            int file_open()
            {
                m_file = fopen(m_path.c_str(), "r");
            }
            // 关闭文件
            int file_close()
            {
                if(m_is_open)
                {
                    fclose(m_file);
                    m_file = NULL;
                    m_is_open = false;
                }
            }
            // 文件大小
            int file_size()
            {
                struct stat file_stat;
                int ret = stat(m_path.c_str(), &file_stat);
                return file_stat.st_size;
            }
            // 文件权限
            int file_mod()
            {
                struct stat file_stat;
                int ret = stat(m_path.c_str(), &file_stat);
                return file_stat.st_mode;
            }
            // 重新设置文件路径
            int reset_path(std::string v_path)
            {
                m_path = v_path;
            }
            int access_time()
            {
                struct stat file_stat;
                int ret = stat(m_path.c_str(), &file_stat);
                return file_stat.st_atime;
            }
            int change_time()
            {
                struct stat file_stat;
                int ret = stat(m_path.c_str(), &file_stat);
                return file_stat.st_ctime;
            }
            int modify_time()
            {
                struct stat file_stat;
                int ret = stat(m_path.c_str(), &file_stat);
                return file_stat.st_mtime;
            }
        private:
            std::string m_path;
            bool m_is_open;
            FILE* m_file;
    };
}